<?php

class Model_ServiceEmailserving extends Model_Service
{

	public function loginCheck($dpt_id, $dpt_account_login, $password="")
	{
		$service_info = $this->getServiceInfo($dpt_id);
			
		$dbconn = $this->getServiceConn($dpt_id);
		//Zend_Debug::dump($dbconn);
		//Zend_Debug::dump($service_info);
		if (isset($dbconn['all']))
		{
			$select  = $dbconn['all']->select()->from('users');
			$select->where('login = ?', $dpt_account_login);
			$select->where('nid = 1');
					
			$user = $dbconn['all']->fetchRow($select);
			//Zend_Debug::dump($user);
			$passwd = md5($password);
					
			if (!$user)
			{
				$rs['status'] = false;
				$rs['message'] = 'account not found';
			}
			else
			{
			   	$status = $user['status'];
         		$passwd_stored = $user['passwd'];
           
				if ($status =='disabled')
		     	{
		        	$rs['status'] = false;
            		$rs['message'] = 'account not active';
		     	}
		     	elseif ($password && $passwd !=$passwd_stored)
		    	{
		        	$rs['status'] = false;
            		$rs['message'] = 'wrong password';
		     	}
		     	else
		     	{
		        	$rs['status'] = true;
            		$rs['dpt_account_id'] = $user['id'];
           	 		$rs['dpt_account_login'] = $dpt_account_login;
           	 		$rs['role'] = $user['role'];
           	 		$rs['email'] = '';
           	 		
            		$rs['login_area'] = 'member';
            
		     	}    
			}
		}
		else
		{
			$rs['status'] = false;
     		$rs['message'] = 'service connection error';
		}

		return $rs; // return OK if passed
	}
	

	public function createAccount($user_id, $dpt_id, $profile)
	{
		$password = Model_User::str_makerand(6,8); //create a random password for remote account
		
		$params=array();
		$params['password']=$password;

		if (is_array($profile)){
			foreach($profile as $key => $value){
				switch($key){
					case 'first_name':
					case 'last_name':
					case 'email':
					case 'company':
					case 'phone':
					case 'address':
					case 'city':
					case 'state':
					case 'zipcode':
					case 'country':
					case 'website':
						if (!empty($value)) $params[ $key ] = $value;
						break;
					default: break;
				}
			}			
		}
		$email = $profile['email'];
		$firstname = $profile['first_name'];
		$lastname = $profile['last_name'];
		
		//$remote_call = 'http://member.emailserving.local/member/api/xmlrpc?key=b84abe02b3497fceb5426bca866e9d83'; //api key for test server
		//$remote_call = 'http://member.emailserving.com/member/api/xmlrpc?key=258c374ed0e78f26e7a9bee6a7a91bae'; //api key for .com server
		$remote_call = 'http://member.emailserving.com/member/api/xmlrpc?key=b84abe02b3497fceb5426bca866e9d83';  //api key for com server
		$client = new Zend_XmlRpc_Client($remote_call);
		if($client->getProxy()->hasUser($email)){ 
			return false;
		}else{
			$reply = $client->getProxy()->addUser($email, $password, $firstname, $lastname);
			if($reply['success']) { 
				$info = $reply['info'];
				$account_id = $info['id'];
				
				if ($account_id>0){
					$login_account = $email;
					$dpt_login_arr=array();
					$dpt_login_arr['dpt_account_login'] = $login_account;
					$dpt_login_arr['login_area'] = 'member';
					$dpt_login_arr['email'] = $profile['email'];
					$dpt_login_arr['password'] = $password;
					$dpt_login_arr['admin_user_id'] = '';
					$dpt_login_arr['url'] = $_SERVER['REQUEST_URI'];
					$dpt_account_id = $account_id;
					
					$model_client = new Model_UserClient();
					$result = $model_client->linkClientService($user_id, $dpt_id, $dpt_account_id, $dpt_login_arr);
					if ($result){
						$ss = new Zend_Session_Namespace('service');
						$ss->link_result = 'success';
						$ss->link_dpt_id = $dpt_id;
						$ss->link_login_account = $login_account;
						$ss->link_dpt_account_id = $account_id;
				
						return true;
					}else 
						return false;			
				}else{
					return false;
				}
				
			}else{
				return false;
			}
			
		}
	}
	
}